import BigNumber from "bignumber.js"
import { Token } from "@app/models/token"
import { FindManyOptions } from 'typeorm'

const Tokens = new class {
    async allPending() {
        const options: FindManyOptions = {
            where: {pending: true},
            order: {id: 'ASC'},
        }
        return await Token.find(options)
    }

    async allAvailable() {
        const options: FindManyOptions = {
            where: {pending: false, receiptStatus: true},
            order: {id: 'ASC'},
        }
        return await Token.find(options)
    }

    async setContractAddress(id: number, address: string) {
        const entity = {
            pending: false,
            receiptStatus: true,
            contract: address
        }
        return await Token.getRepository().update({id: id}, entity)
    }

    async setReceiptStatusAsFail(id: number) {
        const entity = {
            pending: false,
            receiptStatus: false,
        }
        return await Token.getRepository().update({id: id}, entity)
    }

    async insert(symbol: string, initialAmount: BigNumber, hash: string) {
        const record = new Token()
        record.symbol = symbol
        record.initialAmount = initialAmount
        record.hash = hash
        record.pending = true
        return await record.save()
    }
}()

export { Tokens }
